const http = require("http");
const https = require("https");
const fs = require("fs");
const qs = require("querystring");
 
// 请求数据
// http://localhost:3000/api1?type=goods
// 注册
// http://localhost:3000/api1?type=register&username=用户名&password=密码

// http://localhost:3000/api1?type=login&username=用户名&password=密码


http.createServer((req,res)=>{
    if(req.url !== "/favicon.ico"){
        if(req.url.includes("/api1")){
            dataRequire(req, res)
        }else{
            fileRequire(req, res)
        }
    }
}).listen(3000 ,function(){
    console.log("开启成功");
});


function dataRequire(req, res){
    let str = "";
    req.on("data",(c)=>{
        str += c
    })
    req.on("end",()=>{
        let dataObj = null;
        if(str){
            dataObj = qs.parse(str);
        }else{
            dataObj = qs.parse(req.url.split("?")[1]);
        }
        // console.log(dataObj);
        switch(dataObj.type){
            case "goods":
                goods(req, res, dataObj);break;
            case "register":
                register(req,res,dataObj);break; 
                case "login":
                    login(req,res,dataObj);break;    
        }
    })
}
const userMsg = [];

function login(req,res,dataObj){
    let i = 0;
    const flag = userMsg.some((val,idx)=>val.username === dataObj.username

     );
     console.log(flag);
     const obj = {};
     if(flag){ 
         if(userMsg[i].password===dataObj.password){
                    //   登录成功
                    obj.code=1;
                    obj.title="登录成功";
                    obj.data=dataObj.username

         }
         else{
            //    密码不正确
            obj.code=0;
            obj.title="密码不对";
            obj.data="NOT DATA"
         }
          

     }else{
        //  账号不存在
        obj.code=2;
        obj.title="账号不存在";
        obj.data="NOT DATA"
     }
    
     res.write(JSON.stringify(obj))
    res.end();

}
  

function register(req,res,dataObj){
    // console.log("正在注册");
    const flag = userMsg.some(val=>val.username === dataObj.username);
    const obj = {};
    if(flag){
        // 用户名重复，准备返回失败信息
        obj.code = 0;
        obj.title = "用户名重复";
        obj.data = "NOT DATA";
    }else{
        // 注册成功，添加用户信息
        userMsg.push({
            username:dataObj.username,
            password:dataObj.password
        })
        // 并准备返回成功信息
        obj.code = 1;
        obj.title = "注册成功";
        obj.data = "NOT DATA";
    }
    // 返回信息
    res.write(JSON.stringify(obj));
    res.end();
    console.log(userMsg);
}


function goods(req, res, dataObj){
    fs.readFile("./data/goods.json","utf-8",(err,data)=>{
        let msg;
        if(err){
            msg = {code:400,title:"请求失败",data:"NOT DATA"}
        }else{
            console.log("888");
            msg = {code:200,title:"请求成功",data:JSON.parse(data)}
        }
        res.write(JSON.stringify(msg));
        res.end();
    })
}

function fileRequire(req, res){
    const myURL = new URL("http://localhost:3000" + req.url);
    fs.readFile("./www" + myURL.pathname,(err, data)=>{
        if(err){
            res.write("404");
        }else{
            res.write(data);
        }
        res.end();
    })
}

// jk1为接口1路径，依次类推，goods为后端情求数据执行函数
  const obj ={};
// function goods(req, res, dataObj){
//     fs.readFile("jk1","utf-8",(err,data)=>{   
//             obj.data1=JSON.parse(data)  
            
//     })
//     fs.readFile("jk2","utf-8",(err,data)=>{
       
//             obj.data2=JSON.parse(data)        
       
//     })
//     fs.readFile("jk3","utf-8",(err,data)=>{  
//             obj.data3=JSON.parse(data)  
        
//     });
//     res.write(JSON.stringify(msg));
//     res.end();
// }
